﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using BaseThing.Engine;

namespace BaseThing.Game
{

    class AnimatedObject : GameObject
    {
        enum DIR
        {
            UP = 0,
            DOWN,
            RIGHT,
            LEFT
        };

        Sprite sprite = null;
        DIR lastDir = DIR.RIGHT;

        //DIRECTION ANIMATIONS - Point(Row, Col) en SpriteSheet
        Point[] walkAnimations = new Point[4];
        Point[] idleAnimations = new Point[4];


        public AnimatedObject(Vector2 Position, string assetName, int cols, int rows, int fps, Color color)
            : base()
        {
            sprite = new Sprite(assetName, cols, rows, fps);
            sprite.SetMask(color);
            sprite.Position = Position;
            this.Position = Position;

            sprite.SetAnimation(walkAnimations[(int)DIR.DOWN]);
            AddSprite(sprite);
        }

        //Animación WALK. Más de un frame por animación
        public void DirWalkAnimations(Point pointUp, Point pointDown, Point pointRight, Point pointLeft)
        {
            walkAnimations[(int)DIR.UP] = pointUp;
            walkAnimations[(int)DIR.DOWN] = pointDown;
            walkAnimations[(int)DIR.RIGHT] = pointRight;
            walkAnimations[(int)DIR.LEFT] = pointLeft;
        }

        //Animación IDLE. Más de un frame por animación
        public void DirIdleAnimations(Point pointUp, Point pointDown, Point pointRight, Point pointLeft)
        {
            idleAnimations[(int)DIR.UP] = pointUp;
            idleAnimations[(int)DIR.DOWN] = pointDown;
            idleAnimations[(int)DIR.RIGHT] = pointRight;
            idleAnimations[(int)DIR.LEFT] = pointLeft;
        }

        //Animación IDLE. Sólo un frame por animación
        public void DirIdleAnimations(int frameUp, int frameDown, int frameRight, int frameLeft)
        {
            idleAnimations[(int)DIR.UP] = new Point(frameUp, frameUp);
            idleAnimations[(int)DIR.DOWN] = new Point(frameDown, frameDown);
            idleAnimations[(int)DIR.RIGHT] = new Point(frameRight, frameRight);
            idleAnimations[(int)DIR.LEFT] = new Point(frameLeft, frameLeft);
        }

        public override void Update(GameTime gameTime)
        {

            bool keyPressed = false;

            if (Keyboard.GetState().IsKeyDown(Keys.W))
            {
                sprite.SetAnimation(walkAnimations[(int)DIR.UP]);
                Move(new Vector2(0, -1));
                lastDir = DIR.UP;
                keyPressed = true;
            }

            else if (Keyboard.GetState().IsKeyDown(Keys.S))
            {
                sprite.SetAnimation(walkAnimations[(int)DIR.DOWN]);
                Move(new Vector2(0, 1));
                lastDir = DIR.DOWN;
                keyPressed = true;
            }

            else if (Keyboard.GetState().IsKeyDown(Keys.D))
            {
                sprite.SetAnimation(walkAnimations[(int)DIR.RIGHT]);
                Move(new Vector2(1, 0));
                lastDir = DIR.RIGHT;
                keyPressed = true;
            }

            else if (Keyboard.GetState().IsKeyDown(Keys.A))
            {
                sprite.SetAnimation(walkAnimations[(int)DIR.LEFT]);
                Move(new Vector2(-1, 0));
                lastDir = DIR.LEFT;
                keyPressed = true;
            }

            if (!keyPressed)
            {
                sprite.SetAnimation(idleAnimations[(int)lastDir]);
            }
   
             base.Update(gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        { 
            base.Draw(spriteBatch);
        }
    }
}
